package com.zx.idc.backend.gui.base.house;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.base.house.service.BaseHouseOfficerReportService;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.ds.base.house.entity.BaseHouseOfficer;
import com.zx.idc.ds.base.house.service.IBaseHouseOfficerService;
import com.zx.idc.ds.base.house.vo.BaseHouseOfficerVo;
import com.zx.idc.ds.common.constant.DataFeatureConstant;
import com.zx.idc.ds.common.constant.DelFlagConstant;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 机房负责人信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseHouseOfficer")
public class BaseHouseOfficerController extends BasicBaseApi<IBaseHouseOfficerService, BaseHouseOfficer> implements BaseExcelApi<IBaseHouseOfficerService, BaseHouseOfficerVo> {
    @Autowired
    private BaseHouseOfficerReportService baseHouseOfficerReportService;

    /**
     * 查询所有的机房负责人
     *
     * @return 机房负责人列表
     */
    @GetMapping("/select-list")
    @RequiresPermissions("basic-data:base-house-officer")
    public Result<List<BaseHouseOfficer>> list() {
        return success(service.selectList(new EntityWrapper<BaseHouseOfficer>().setSqlSelect("id", "name")));
    }

    @GetMapping("/query-page")
    @RequiresPermissions("basic-data:base-house-officer")
    public Result<Page<BaseHouseOfficerVo>> page(Page<BaseHouseOfficerVo> page, BaseHouseOfficerVo baseHouseOfficerVo) {
        return success(service.pageByParam(page, baseHouseOfficerVo));
    }

    @GetMapping("/get/{id}")
    @RequiresPermissions("basic-data:base-house-officer")
    public Result<BaseHouseOfficer> get(@PathVariable Long id) {
        return success(service.selectById(id));
    }

    @PostMapping("/insert-report/{id}")
    @RequiresPermissions("basic-data:base-house-officer:edit")
    public Result<Boolean> insertReport(@PathVariable Long id) {
        return success(baseHouseOfficerReportService.insertReport(id));
    }

    @PostMapping("/update-report/{id}")
    @RequiresPermissions("basic-data:base-house-officer:edit")
    public Result<Boolean> updateReport(@PathVariable Long id) {
        return success(baseHouseOfficerReportService.updateReport(id));
    }

    @Override
    @RequiresPermissions("basic-data:base-house-officer:edit")
    public Result<Boolean> add(@RequestBody BaseHouseOfficer entity) {
        return super.add(entity
            .setDelFlag(DelFlagConstant.NORMAL)
            .setReportStatus(ReportStatusConstant.NOT_REPORTED)
            .setDataFeature(DataFeatureConstant.NEW)
        );
    }

    @Override
    @RequiresPermissions("basic-data:base-house-officer:edit")
    public Result<Boolean> update(@RequestBody BaseHouseOfficer entity) {
        String msg;
        try {
            return success(service.updateHouseOfficer(entity));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("修改负责人异常", e);
        }
        return error(msg);
    }

    @PostMapping("/delete/batch")
    @RequiresPermissions("basic-data:base-house-officer:edit")
    public Result<Boolean> delete(@RequestBody List<Long> idList) {
        return success(service.deleteBatchByIds(idList));
    }
}

